﻿using System;
using UnityEngine;

namespace JH
{
    public static class SynchronousTransformExtension
    {
        /// <summary>
        ///  同步Transform
        /// </summary>
        public static void SynchronousTransformEm(this Transform transform, Transform targetTransform)
        {
            if (!transform || !targetTransform)
                throw new ArgumentNullException(nameof(targetTransform), "Transform 不能为空");
            transform.position = targetTransform.position;
            transform.rotation = targetTransform.rotation;
            transform.localScale = targetTransform.localScale;
        }

        /// <summary>
        ///  同步LocalTransform
        /// </summary>
        public static void SynchronousLocalTransformEm(this Transform transform, Transform targetTransform)
        {
            if (!transform || !targetTransform)
                throw new ArgumentNullException(nameof(targetTransform), "Transform 不能为空");
            transform.localPosition = targetTransform.localPosition;
            transform.localRotation = targetTransform.localRotation;
            transform.localScale = targetTransform.localScale;
        }

        /// <summary>
        ///  同步RectTransform
        /// </summary>
        public static void SynchronousRectTransformEm(this RectTransform rect1, RectTransform rect2)
        {
            if (!rect1 || !rect2)
                throw new ArgumentNullException(nameof(rect2), "RectTransform 不能为空");
            rect1.anchorMax = rect2.anchorMax;
            rect1.anchorMin = rect2.anchorMin;
            rect1.pivot = rect2.pivot;
            rect1.SynchronousTransformEm(rect2);
            rect1.anchoredPosition = rect2.anchoredPosition;
            rect1.sizeDelta = rect2.sizeDelta;
        }

        /// <summary>
        /// 同步LocalRectTransform
        /// </summary>
        public static void SynchronousLocalRectTransformEm(this RectTransform rect1, RectTransform rect2)
        {
            if (!rect1 || !rect2)
                throw new ArgumentNullException(nameof(rect2), "RectTransform 不能为空");
            rect1.anchorMax = rect2.anchorMax;
            rect1.anchorMin = rect2.anchorMin;
            rect1.pivot = rect2.pivot;
            SynchronousLocalTransformEm(rect1, rect2);
            rect1.anchoredPosition = rect2.anchoredPosition;
            rect1.sizeDelta = rect2.sizeDelta;
        }
    }
}